package com.everflourish.yeah100.utils;

import java.util.regex.Pattern;

/**
 * 字符串工具类
 *
 * @author pf
 * @date 2015-3-18
 */
public class StringUtil {

    /**
     * 判断字符串是否是空或者是null
     *
     * @param str
     * @return
     */
    public static boolean stringIsNull(String str) {
        boolean isNull = true;
        if (str == null || str.trim().equals("")) {
            isNull = true;
        } else {
            isNull = false;
        }
        return isNull;
    }

    /**
     * 判断字符串是否为非负整数
     *
     * @param str
     * @return
     */
    public static boolean isNonNegativeNumber(String str) {
        String regex = "^\\d+$";
        boolean result = Pattern.matches(regex, str);
        return result;
    }

    /**
     * 判断字符串是否是汉字
     *
     * @param str
     * @return
     */
    public static boolean isCharacter(String str) {
        String regex = "^[\u4E00-\u9FFF]+$";
        boolean result = Pattern.matches(regex, str);
        return result;
    }

    /**
     * 判断用户名是否正确 正则表达式 用户名：6~20个字符，可使用字母、数字、下划线，需以字母开头
     *
     * @param userName
     * @return
     */
    public static boolean isUserName(String userName) {
        String regex = "^[a-zA-Z][a-zA-Z0-9_]{5,19}$";
        boolean result = Pattern.matches(regex, userName);
        return result;
    }

    public static boolean isPut(String userName) {
        String regex = "^[A-Za-z0-9\\u4e00-\\u9fa5\\（\\）\\【\\】\\(\\)\\[\\]\\{\\}\\_-]{2,16}";
        boolean result = Pattern.matches(regex, userName);
        return result;
    }

    /**
     * 判断字符串是否是邮箱格式
     *
     * @param emailStr
     * @return true or false
     */
    public static boolean isEmail(String emailStr) {
        String regex = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
        boolean result = Pattern.matches(regex, emailStr);
        return result;
    }

    /**
     * 判断手机号码是否正确
     *
     * @param phoneStr
     * @return
     */
    public static boolean isPhoneNo(String phoneStr) {
        String regex = "1[3|4|5|7|8|][0-9]{9}";
        boolean result = Pattern.matches(regex, phoneStr);
        return result;
    }

    /**
     * 判断是否是密码 正则表达式。密码：6~20个字符，由数字和字母，区分大小写
     *
     * @param password
     * @return
     */
    public static boolean isPassword(String password) {
        String regex = "^[A-Za-z0-9]{6,20}$";
        boolean result = Pattern.matches(regex, password);
        return result;
    }

    /**
     * 判断是否考试名称 正则表达式。请输入不超过15个字的内容
     *
     * @param name
     * @return
     */
    public static boolean isExamName(String name) {
        String regex = "[^\\\\s]{2,16}";
        boolean result = Pattern.matches(regex, name);
        return result;
    }

    /**
     * 判断是否昵称 正则表达式。请输入不超过32个字的内容
     *
     * @param name
     * @return
     */
    public static boolean isNickname(String name) {
        String regex = "[^\\\\s]{0,32}";
        boolean result = Pattern.matches(regex, name);
        return result;
    }

    /**
     * 判断是否是座右铭 正则表达式。请输入不超过100个字的内容
     *
     * @param name
     * @return
     */
    public static boolean isMotto(String name) {
        String regex = "[^\\\\s]{0,100}";
        boolean result = Pattern.matches(regex, name);
        return result;
    }

    /**
     * 判断是否考试名称 正则表达式。请输入不超过15个字的内容
     *
     * @param name
     * @return
     */
    public static boolean isTopicName(String name) {
//        String regex = "[^\\\\s]{1,16}";
        String regex = "^{0,16}";
        boolean result = Pattern.matches(regex, name);
        return result;
    }

    /**
     * 判断字符串是否不超过20个字符，且至少有一个字符不为空字符
     *
     * @param examName
     * @return
     */
    public static boolean is20Char(String examName) {
        String regex = "^.{0,20}";
//		examName = examName.replace(" ", "");
        boolean result = Pattern.matches(regex, examName);
        return result;
    }

    /**
     * 判断手机号码是否正确
     *
     * @param phoneStr
     * @return
     */
    public static boolean isPhone(String phoneStr) {
        if (phoneStr.length() != 11) {
            return false;
        }
        return true;
    }

    /**
     * 字符串转换为double类型
     *
     * @param strDouble
     * @param defaultDouble
     * @return
     */
    public static double parseDouble(String strDouble, double defaultDouble) {
        double value = 0;
        if (stringIsNull(strDouble)) {
            return defaultDouble;
        }
        try {
            value = Double.parseDouble(strDouble);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            value = defaultDouble;
        }
        return value;
    }

    /**
     * 将字符串转换为整型，如果报错将作物默认
     *
     * @param strInteger
     * @param errorValue
     * @return
     */
    public static Integer parseInt(String strInteger, int errorValue) {
        if (strInteger != null && strInteger.trim().equals("")) {
            return errorValue;
        }
        int value = errorValue;
        try {
            value = Integer.parseInt(strInteger);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return value;
    }

    /**
     * 将字符串转换为Float，如果报错将作物默认
     *
     * @param strInteger
     * @param errorValue
     * @return
     */
    public static Float parseFloat(String strInteger, Float errorValue) {
        Float value = errorValue;
        try {
            value = Float.parseFloat(strInteger);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return value;
    }

    /**
     * 判断不超过9个字符（只支持中英文字符）
     *
     * @param examName
     * @return
     */
    public static boolean is9Str(String examName) {
        String regex = "[0-9A-z\\u4E00-\\u9FA5]{0,9}";
//		examName = examName.replace(" ", "");
        boolean result = Pattern.matches(regex, examName);
        return result;
    }
}
